package com.chao.demo.controller;

import com.chao.demo.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(value = "Redis测试API", tags = {"Redis测试API"})
@RestController
public class RedisTestController {

    @Autowired
    private RedisUtil redisUtil;

    @ApiOperation(value = "测试redis", notes = "测试redis")
    @GetMapping("/redis")
    public String redisTest() {
        // redisUtil.hPut("HOT_POINT_DATA" + ":1", "blog_likes", "30");
        return "保存成功";
    }

    @ApiOperation(value = "保存到数据库和redis缓存中", notes = "保存到数据库和redis缓存中")
    @GetMapping("/saveBDAndRedis")
    public String saveBDAndRedis(@ApiParam(name = "likes", value = "点赞数", required = false) @RequestParam(name = "like", defaultValue = "0", required = false) String likes) {
        System.out.println("likes = " + likes);
        Object dz = redisUtil.hGet("HOT_POINT_DATA" + ":1", "blog_dz");
        if (dz == null) {
            // 保存数据库 save()
        }
        // 更新缓存
        redisUtil.hPut("HOT_POINT_DATA" + ":1", "blog_dz", likes);
        return "保存成功";
    }

    public static void main(String[] args) {
        double f = 2.3;
        for (int i = 0; i < args.length; i++) {
            System.out.println(args[i]);
        }
    }
}
